package com.azure.service.Report;

import com.azure.entity.ACategoryDailyReport;
import com.azure.entity.ACategoryMonthlyReport;
import com.azure.entity.TCategory;
import com.azure.mapper.bigstore.TCategoryMapper;
import com.azure.mapper.bigstore.TOrderitemMapper;
import com.azure.mapper.bigstore.TProductMapper;
import com.azure.mapper.report.ACategoryDailyReportMapper;
import com.azure.mapper.report.ACategoryMonthlyReportMapper;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhuaibin on 2018/3/13.
 */
@Service
public class CategoryReportService {
    @Autowired
    private TProductMapper tProductMapper;
    @Autowired
    private TCategoryMapper tCategoryMapper;
    @Autowired
    private TOrderitemMapper tOrderitemMapper;
    @Autowired
    private ACategoryDailyReportMapper aCategoryDailyReportMapper;
    @Autowired
    private ACategoryMonthlyReportMapper aCategoryMonthlyReportMapper;

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMM");
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMM");

    public void totalDailyReport(LocalDate date){
        //pid--hot
        List<JSONObject> jsonObjects = tOrderitemMapper.totalDailyReport(date);
        if(jsonObjects==null || jsonObjects.size() == 0){
            System.out.println("=== CategoryReportService totalDailyReport  !!!empty list!!! tOrderitemMapper.totalDailyReport() ===");
            return;
        }
        Map<Long,Integer> pid_hot_map = new HashMap<>();
        List<Long> pids = new ArrayList<>();
        for(JSONObject object : jsonObjects){
            pid_hot_map.put(Long.valueOf(object.getAsString("pid")), Integer.valueOf(object.getAsString("hot")));
            pids.add(Long.valueOf(object.getAsString("pid")));
        }
        //pid--cid
        List<JSONObject> pidCategoryList = tProductMapper.selectByPids(pids);
        Map<Long, Long> pid_category_map = new HashMap<>();
        List<Long> categoryIds = new ArrayList<>();
        for(JSONObject object : pidCategoryList){
            pid_category_map.put(Long.valueOf(object.getAsString("id")),Long.valueOf(object.getAsString("category")));
            categoryIds.add(Long.valueOf(object.getAsString("category")));
        }
        //cid--name
        List<JSONObject> cid_name_list = tCategoryMapper.selectByIds(categoryIds);
        Map<Long,String> cid_name_map = new HashMap<>();
        for(JSONObject object : cid_name_list){
            cid_name_map.put(Long.valueOf(object.getAsString("id")), object.getAsString("name"));
        }
        //pid--cname
        Map<Long,String> pid_cname_map = new HashMap<>();
        for(Long pid : pid_category_map.keySet()){
            pid_cname_map.put(pid, cid_name_map.get(   pid_category_map.get(pid)   ));
        }
        //cname--num
        Map<String,Integer> cname_num_map = new HashMap<>();
        for(Long pid : pid_hot_map.keySet()){
            int num = cname_num_map.get(pid_cname_map.get(pid))==null ? 0 : cname_num_map.get(pid_cname_map.get(pid));
            cname_num_map.put(pid_cname_map.get(pid), num + pid_hot_map.get(pid));
        }
        for(String cname : cname_num_map.keySet()){
            aCategoryDailyReportMapper.insert(new ACategoryDailyReport(dateTimeFormatter.format(date),
                    cname, Long.valueOf(cname_num_map.get(cname).toString()) ) );
        }
        System.out.println("====== CategoryReportService totalDailyReport  cname_num_map:"+cname_num_map);
    }

    public void totalMonthlyReport(LocalDate startDate, LocalDate stopDate){
        System.out.println("startDate: "+startDate+"  stopDate: "+stopDate);
        List<JSONObject> jsonObjects = tOrderitemMapper.totalMonthlyReport(startDate,stopDate);
        if(jsonObjects==null || jsonObjects.size() == 0){
            System.out.println("=== CategoryReportService totalMonthlyReport  !!!empty list!!! tOrderitemMapper.totalMonthlyReport() ===");
            return;
        }
        Map<Long,Integer> pid_hot_map = new HashMap<>();
        List<Long> pids = new ArrayList<>();
        for(JSONObject object : jsonObjects){
            pid_hot_map.put(Long.valueOf(object.getAsString("pid")), Integer.valueOf(object.getAsString("hot")));
            pids.add(Long.valueOf(object.getAsString("pid")));
        }
        //pid--cid
        List<JSONObject> pidCategoryList = tProductMapper.selectByPids(pids);
        Map<Long, Long> pid_category_map = new HashMap<>();
        List<Long> categoryIds = new ArrayList<>();
        for(JSONObject object : pidCategoryList){
            pid_category_map.put(Long.valueOf(object.getAsString("id")),Long.valueOf(object.getAsString("category")));
            categoryIds.add(Long.valueOf(object.getAsString("category")));
        }
        //cid--name
        List<JSONObject> cid_name_list = tCategoryMapper.selectByIds(categoryIds);
        Map<Long,String> cid_name_map = new HashMap<>();
        for(JSONObject object : cid_name_list){
            cid_name_map.put(Long.valueOf(object.getAsString("id")), object.getAsString("name"));
        }
        //pid--cname
        Map<Long,String> pid_cname_map = new HashMap<>();
        for(Long pid : pid_category_map.keySet()){
            pid_cname_map.put(pid, cid_name_map.get(   pid_category_map.get(pid)   ));
        }
        //cname--num
        Map<String,Integer> cname_num_map = new HashMap<>();
        for(Long pid : pid_hot_map.keySet()){
            int num = cname_num_map.get(pid_cname_map.get(pid))==null ? 0 : cname_num_map.get(pid_cname_map.get(pid));
            cname_num_map.put(pid_cname_map.get(pid), num + pid_hot_map.get(pid));
        }
        for(String cname : cname_num_map.keySet()){
            aCategoryMonthlyReportMapper.insert(new ACategoryMonthlyReport(dateTimeFormatter2.format(startDate),
                    cname, Long.valueOf(cname_num_map.get(cname).toString()) ) );
        }
        System.out.println("====== CategoryReportService totalMonthlyReport  cname_num_map:"+cname_num_map);
    }



    public Map<String,Object> getDailyCategoryData(String date){
        List<String> categoryNames = new ArrayList<>();
        List<Long> categoryHot = new ArrayList<>();
        String categoryDate = "\'日数据\'";

        Map<String,Object> categoryData = new HashMap<>();
        List<ACategoryDailyReport> dailyCategoryData = aCategoryDailyReportMapper.getDailyCategoryData(date);
        if(dailyCategoryData==null || dailyCategoryData.size()==0){
            System.out.println("=== CategoryReportService getDailyCategoryData  !!!empty list!!! aCategoryDailyReportMapper.getDailyCategoryData() ===");
            return categoryData;
        }
        for(ACategoryDailyReport aCategoryDailyReport : dailyCategoryData){
            categoryNames.add("\""+aCategoryDailyReport.getCategory()+"\"");
            categoryHot.add(aCategoryDailyReport.getHot());
            categoryDate = "\""+aCategoryDailyReport.getDate()+"日数据\"";
        }
        categoryData.put("categoryNames",categoryNames);
        categoryData.put("categoryHot",categoryHot);
        categoryData.put("categoryDate",categoryDate);
        return categoryData;
    }

    public Map<String,Object> getMonthlyCategoryData(String date){
        List<String> categoryNames = new ArrayList<>();
        List<Long> categoryHot = new ArrayList<>();
        String categoryDate = "\'月数据\'";

        Map<String,Object> categoryData = new HashMap<>();
        List<ACategoryMonthlyReport> monthlyCategoryData = aCategoryMonthlyReportMapper.getMonthlyCategoryData(date);
        if(monthlyCategoryData==null || monthlyCategoryData.size()==0){
            System.out.println("=== CategoryReportService getDailyCategoryData  !!!empty list!!! aCategoryDailyReportMapper.getMonthlyCategoryData() ===");
            return categoryData;
        }
        for(ACategoryMonthlyReport aCategoryMonthlyReport : monthlyCategoryData){
            categoryNames.add("\""+aCategoryMonthlyReport.getCategory()+"\"");
            categoryHot.add(aCategoryMonthlyReport.getHot());
            categoryDate = "\'"+aCategoryMonthlyReport.getDate()+"月数据\'";
        }
        categoryData.put("categoryNames",categoryNames);
        categoryData.put("categoryHot",categoryHot);
        categoryData.put("categoryDate",categoryDate);
        return categoryData;
    }

}
